# coding:utf-8
"""
description:Sidebar》Devices Page列表
"""
import os
import random

from appium.webdriver.common import mobileby
from common.Gesture_Mainpulation import gesture_mainpulation
from pages.Public_elements import public_elements
from common.Get_Yaml import get_yaml
from config.GlobalParameter import TEST_Element_YAML

testElements = get_yaml(os.path.join(TEST_Element_YAML, "Devices", "Devices.yaml"))


class Devices_page(public_elements):
    by = mobileby.MobileBy()

    # 点击右上角的+号按钮，同dashboard page右上角Add device 按钮
    btnDashboardAdd_loc = (by.ID, testElements.get_elementInfo(0))

    def click_btnDashboardAdd(self):
        """点击右上角的+号按钮，同dashboard page右上角Add device 按钮"""
        self.find_element(*self.btnDashboardAdd_loc).click()

    # 点击room控件内右上角的delete按钮后，该room控件变为编辑选择状态,集合
    btnConnectedDevicesDelete_loc = (by.ID, testElements.get_elementInfo(1))

    def get_btnConnectedDevicesDelete(self):
        """点击room控件内右上角的delete按钮后，该room控件变为编辑选择状态，集合"""
        return self.find_elements(*self.btnConnectedDevicesDelete_loc)

    # 获取room控件下的room集合
    DashboardDeviceIcon_loc = (by.ID, testElements.get_elementInfo(2))

    def get_DashboardDeviceIcon(self):
        """获取room控件下的room集合"""
        return self.find_elements(*self.DashboardDeviceIcon_loc)

    # 获取room控件的room name
    ConnectedDevicesRoom_loc = (by.ID, testElements.get_elementInfo(3))

    def get_ConnectedDevicesRoom(self):
        """获取room控件的room name"""
        return self.find_elements(*self.ConnectedDevicesRoom_loc)

    # 上下滑动room控件，查看所有room和room下的device
    DevicesRoomScroll_loc = (by.ID, testElements.get_elementInfo(4))

    def swipe_DevicesRoomScroll(self):
        """上下滑动room控件，查看所有room和room下的device"""
        xy = gesture_mainpulation().centreXAndY(self.find_element(*self.DevicesRoomScroll_loc))
        self.driver.swipe(xy[0], xy[3] - 50, xy[0], xy[5] + 50, 200)

    def func_SwipeRoom(self, length):
        """
        滑动Devices page页面room 控件，展示room name
        :param length: 滑动room 控件的次数
        :return: room name list，会有重复
        """
        list = []
        for i in range(length):
            self.swipe_DevicesRoomScroll()
            for index, row in enumerate(self.get_ConnectedDevicesRoom()):
                list.append(row.text)
        return list

    # 获取底部floor name
    FloorName_loc = (by.ID, testElements.get_elementInfo(5))

    def get_FloorName(self):
        """获取底部floor name"""
        return self.find_elements(*self.FloorName_loc)

    # 左右滑动floor控件，展示所有floor name
    ConnectedDevicesFloor_loc = (by.ID, testElements.get_elementInfo(6))

    def swipe_ConnectedDevicesFloor(self, leftright="right"):
        """左右滑动floor控件，展示所有floor name"""
        xy = gesture_mainpulation().centreXAndY(self.find_element(*self.ConnectedDevicesFloor_loc))
        if leftright == "right":
            self.driver.swipe(xy[2] - 50, xy[1], xy[4] + 50, xy[1], 200)
        else:
            self.driver.swipe(xy[4] + 50, xy[1], xy[2] - 50, xy[1], 200)

    # 获取room控件编辑状态的单选框。pop-up element见Public.yaml（3，13，14）
    cbDashboardDeviceDelete_loc = (by.ID, testElements.get_elementInfo(7))

    def click_cbDashboardDeviceDelete(self):
        """获取room控件编辑状态的单选框。pop-up element见Public.yaml（3，13，14）"""
        self.find_elements(*self.cbDashboardDeviceDelete_loc).click()
